package com.taotao.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.CookieUtils;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.CartService;
import com.taotao.portal.service.ItemService;

@Service
public class CartServiceImpl implements CartService {
	
	@Autowired
	private ItemService itemService;
	@Override
	// 获取购物车
	public List<CartItem> getCart(HttpServletRequest request) {
		// 从cookie中获取购物车
		String cookie = CookieUtils.getCookieValue(request, "cart", true);
		List<CartItem> list = null;
		if (!StringUtils.isBlank(cookie)) {
			list = JsonUtils.jsonToList(cookie, CartItem.class);
		}
		// 如果没有购物车就创建一个
		if (list == null) {
			list = new ArrayList<CartItem>();
		}
		return list;
	}
	
	// 更新Cart中商品的数量
	@Override
	public TaotaoResult updataCartItem(Long id, Integer num, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

	
		// 从cookie中获取购物车
		List<CartItem> cart = getCart(request);

		// 在购物车中查找购物车项
		CartItem cartItem = null;
		for (CartItem item : cart) {
			if (id.equals(item.getId())) {
				if(num<1){
					return TaotaoResult.build(401, "不能小于1", item.getNum());
				}
				cartItem = item;
				cartItem.setNum(num);
				break;
			}
		}

		// 如果购物车中不存在就调用rest服务查找商品信息并添加到购物车中
		if (cartItem == null) {
			try {
				addItem(id, num, cart);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		storeCartToCookie(cart, request, response);
		return TaotaoResult.ok();
	}

	// 添加商品到Cart中
	@Override
	public TaotaoResult addCartItem(Long id, Integer num, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 从cookie中获取购物车
		List<CartItem> cart = getCart(request);

		// 在购物车中查找购物车项
		CartItem cartItem = null;
		for (CartItem item : cart) {
			if (id.equals(item.getId())) {
				int total = item.getNum() + num;
				cartItem = item;
				cartItem.setNum(total);
				break;
			}
		}

		// 如果购物车中不存在就调用rest服务查找商品信息并添加到购物车中
		if (cartItem == null) {
			try {
				addItem(id, num, cart);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		storeCartToCookie(cart, request, response);

		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult delCartItem(Long id, HttpServletRequest request, HttpServletResponse response) {
		List<CartItem> cart = getCart(request);
		// 在购物车中查找购物车项
		CartItem cartItem = null;
		for (int i=0;i<cart.size();i++) {
			if (id.equals(cart.get(i).getId())) {
				cart.remove(i);
				break;
			}
		}
		storeCartToCookie(cart, request, response);
		return TaotaoResult.ok();
	}

	// 存储购物车到cookie中
	private void storeCartToCookie(List<CartItem> cart, HttpServletRequest request, HttpServletResponse response) {
		String json = JsonUtils.objectToJson(cart);
		CookieUtils.setCookie(request, response, "cart", json, true);
	}



	// 添加num个商品到cart中
	private void addItem(Long id, Integer num, List<CartItem> cart) throws Exception {
//
//		String url = REST_BASE_URL + ITEM_INFO_URL + "/" + id;
//		String json = HttpClientUtil.doGet(url);
//		if (StringUtils.isBlank(json))
//			throw new Exception("获取商品信息失败");
//
//		ItemInfo itemInfo = (ItemInfo) TaotaoResult.formatToPojo(json, ItemInfo.class).getData();
		ItemInfo itemInfo =itemService.getItemInfoById(id);
		
		CartItem cartItem = new CartItem();

		cartItem.setId(id);
		cartItem.setNum(num);
	
		cartItem.setImage(itemInfo.getFirstImage());
		cartItem.setPrice(itemInfo.getPrice());
		cartItem.setTitle(itemInfo.getTitle());
		cart.add(cartItem);
	}

	@Override
	public TaotaoResult clearCart(HttpServletRequest request, HttpServletResponse response) {

		storeCartToCookie(new ArrayList<CartItem>(), request, response);
		return TaotaoResult.ok();
	}

}
